<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class RaoVat_Model extends MY_Model {
    private $entity_name  = 'RaoVat_Entity';

    public function __construct() {
        parent::__construct();
        $this->load->model('entity/raovat_entity', $this->entity_name);
    }

    public function countStickyWidget($cond=array()){
        $select = "a.id";
        $this->db->select($select, true);
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_rao_vat_place_name . ' b', 'a.place_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_rao_vat_highlight_name . ' e', 'e.raovat_id=a.id');
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        if(count($cond)>0){
            $this->db->where($cond);
        } else {
            $this->db->where('a.active_flg', 1);
            $this->db->where('a.deleted_at', null);
            $this->db->where('e.deleted_at', null);
        }
        return $this->db->count_all_results();
    }

    public function getStickyWidget($limit, $offset, $aCol = array(), $cond=array()){
        $select = "
        a.id, a.title, a.slug, a.image, a.description, a.view_time, a.created_at,
        b.name as place_name,
        c.image_folder as image_folder,
        d.title as cate_title, d.slug as cate_slug
        ";
        if (count($aCol) > 0) {
            $select = implode(',', $aCol);
        }
        $this->db->select($select, true);
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_rao_vat_place_name . ' b', 'a.place_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_rao_vat_highlight_name . ' e', 'e.raovat_id=a.id');
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        if(count($cond)>0){
            $this->db->where($cond);
        } else {
            $this->db->where('a.active_flg', 1);
            $this->db->where('a.deleted_at', null);
            $this->db->where('e.deleted_at', null);
        }
        $this->db->order_by('e.created_at', 'DESC');
        $this->db->order_by('e.id', 'DESC');
        $this->db->limit($limit, $offset);
        return $this->db->get()->result('RaoVat_Entity');
    }

    public function countStickyByCategory($category_id=null){
        $this->db->select("COUNT(a.id) as cate_count, a.cate_id as cate_id");
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        if($category_id!=null){
            $this->db->where('d.id', $category_id);
        } else {
            $this->db->group_by('d.id');
            $this->db->order_by('a.id');
        }
        return $this->db->get()->result();
    }

    public function countRaoVat($option=array()){
        $this->db->select('a.id');
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        if(count($option)==0){
            $this->db->where('a.active_flg', 1);
            $this->db->where('a.deleted_at', null);
            $this->db->where('c.deleted_at', null);
            $this->db->where('d.deleted_at', null);
        } else {
            $this->db->where($option);
        }
        return $this->db->count_all_results();
    }

    public function paging($limit, $offset, $aCol = array(), $cond=array()) {
        $select = "
        a.id, a.title, a.slug, a.image, a.description, a.view_time, a.created_at,
        b.name as place_name,
        c.image_folder as image_folder,
        d.title as cate_title, d.slug as cate_slug
        ";
        if (count($aCol) > 0) {
            $select = implode(',', $aCol);
        }
        $this->db->select($select, true);
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_rao_vat_place_name . ' b', 'a.place_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        if(count($cond)>0){
            $this->db->where($cond);
        }
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->order_by('a.id', 'DESC');
        $this->db->limit($limit, $offset);
        return $this->db->get()->result('RaoVat_Entity');
    }

    public function getSingleBySlug($slug, $aSelect=array(), $isAdmin=false) {
        $select = "";
        if(count($aSelect)>0){
            $select = implode(',', $aSelect);
        } else {
            $select = "
            a.id, a.user_id, a.title, a.slug, a.image, a.cate_id , a.description, a.detail, a.view_time, a.custom_info, a.created_at,
            b.name as place,
            c.full_name, c.email, address, c.company, c.phone, c.home_phone, c.skype,c.image_folder,
            d.title as cate_title, d.slug as cate_slug";
        }
        $this->db->select($select, true);
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_rao_vat_place_name . ' b', 'a.place_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        if(!$isAdmin){
            $this->db->where('a.active_flg', 1);
            $this->db->where('a.deleted_at', null);
        }
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('a.slug', $slug);
        return $this->db->get()->first_row('RaoVat_Entity');
    }

    public function getSingleById($id, $aSelect=array(), $isAdmin=false) {
        $select = "";
        if(count($aSelect)>0){
            $select = implode(',', $aSelect);
        } else {
            $select = "
            a.id, a.title, a.slug, a.image, a.cate_id , a.description, a.detail, a.view_time, a.custom_info, a.created_at,
            b.name as place,
            c.full_name, c.email, address, c.company, c.phone, c.home_phone, c.skype,c.image_folder,
            d.title as cate_title, d.slug as cate_slug";
        }
        $this->db->select($select, true);
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_rao_vat_place_name . ' b', 'a.place_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        if(!$isAdmin){
            $this->db->where('a.active_flg', 1);
            $this->db->where('a.deleted_at', null);
        }
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('a.id', $id);
        return $this->db->get()->first_row('RaoVat_Entity');
    }

    public function countRaoVatByCategory($category, $option=array()){
        $this->db->select('a.id');
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        if(count($option)==0){
            $this->db->where('a.active_flg', 1);
            $this->db->where('a.deleted_at', null);
            $this->db->where('c.deleted_at', null);
            $this->db->where('d.deleted_at', null);
            $this->db->where('d.slug', $category);
        } else {
            $this->db->where($option);
        }
        return $this->db->count_all_results();
    }

    public function getByCategory($limit, $offset, $category, $aCol = array()){
        $select = "
        a.id, a.title, a.slug, a.image, a.description, a.view_time, a.created_at,
        b.name as place_name,
        c.image_folder,
        d.title as cate_title, d.slug as cate_slug
        ";
        if (count($aCol) > 0) {
            $select = implode(',', $aCol);
        }
        $this->db->select($select, true);
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_rao_vat_place_name . ' b', 'a.place_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('d.slug', $category);
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->order_by('a.id', 'DESC');
        $this->db->limit($limit, $offset);
        return $this->db->get()->result('RaoVat_Entity');
    }

    /////////////////////////// SHOP ONLY /////////////////////////////////////
    public function shopCountRaoVat($user_id, $option=array()){
        $this->db->select('a.id');
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->where('a.user_id', $user_id);
        if(count($option)>0){
            $this->db->where($option);
        } else {
            $this->db->where('a.active_flg', 1);
            $this->db->where('a.deleted_at', null);
        }
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        return $this->db->count_all_results();
    }

    public function shopPaging($user_id, $limit, $offset, $aCol = array(), $option=array()) {
        $select = "
        a.id, a.title, a.slug, a.image, a.description, a.view_time, a.created_at,a.active_flg,
        b.name as place_name,
        c.image_folder,
        d.title as cate_title, d.slug as cate_slug
        ";
        if (count($aCol) > 0) {
            $select = implode(',', $aCol);
        }
        $this->db->select($select, true);
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_rao_vat_place_name . ' b', 'a.place_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->where('a.user_id', $user_id);
        if(count($option)>0){
            $this->db->where($option);
        } else {
            $this->db->where('a.active_flg', 1);
            $this->db->where('a.deleted_at', null);
        }
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->order_by('a.id', 'DESC');
        $this->db->limit($limit, $offset);
        return $this->db->get()->result('RaoVat_Entity');
    }

    public function shopSearchCountRaoVat($search_key, $user_id, $option=array()){
        $this->db->select('a.id');
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_rao_vat_place_name . ' b', 'a.place_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->where('a.user_id', $user_id);
        $this->db->where('a.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $str_like = "(a.title LIKE '%{$search_key}%' OR b.name LIKE '%{$search_key}%' OR d.title LIKE '%{$search_key}%' OR a.description LIKE '%{$search_key}%')";
        $this->db->where($str_like);
        return $this->db->count_all_results();
    }

    public function shopSearchPaging($search_key, $user_id, $limit, $offset, $aCol = array()) {
        $select = "
        a.id, a.title, a.slug, a.image, a.description, a.view_time, a.created_at,a.active_flg,
        b.name as place_name,
        c.image_folder,
        d.title as cate_title, d.slug as cate_slug
        ";
        if (count($aCol) > 0) {
            $select = implode(',', $aCol);
        }
        $this->db->select($select, true);
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_rao_vat_place_name . ' b', 'a.place_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->where('a.user_id', $user_id);
        $this->db->where('a.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $str_like = "(a.title LIKE '%{$search_key}%' OR b.name LIKE '%{$search_key}%' OR d.title LIKE '%{$search_key}%' OR a.description LIKE '%{$search_key}%')";
        $this->db->where($str_like);
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->order_by('a.id', 'DESC');
        $this->db->limit($limit, $offset);
        return $this->db->get()->result('RaoVat_Entity');
    }

    /**
     * For admin only
     * Add new raovat
     */
    /////////////////////////// ADMIN ONLY /////////////////////////////////////
    public function adminCountRaoVat($option=null){
        $this->db->select('a.id');
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        if(count($option)==0){
            $this->db->where('a.active_flg', 1);
            $this->db->where('a.deleted_at', null);
            $this->db->where('c.deleted_at', null);
            $this->db->where('d.deleted_at', null);
        } else {
            $this->db->where($option);
        }
        return $this->db->count_all_results();
    }

    public function adminPaging($limit, $offset, $aCol = array(), $option=null) {
        $select = "
        a.id, a.title, a.slug, a.image, a.description, a.view_time, a.created_at,a.active_flg, a.deleted_at,
        b.name as place_name, c.image_folder,
        d.title as cate_title, d.slug as cate_slug
        ";
        if (count($aCol) > 0) {
            $select = implode(',', $aCol);
        }
        $this->db->select($select, true);
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_rao_vat_place_name . ' b', 'a.place_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        if(count($option)==0){
            $this->db->where('a.active_flg', 1);
            $this->db->where('a.deleted_at', null);
            $this->db->where('c.deleted_at', null);
            $this->db->where('d.deleted_at', null);
        } else {
            $this->db->where($option);
        }
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->order_by('a.id', 'DESC');
        $this->db->limit($limit, $offset);

        //var_dump($this->db->get()->result('RaoVat_Entity'));die();

        return $this->db->get()->result('RaoVat_Entity');
    }

    public function countAdminSearchPaging($searchKey) {
        $this->db->select('a.id');
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->like('a.title', $searchKey);
        return $this->db->count_all_results();
    }

    public function adminSearchPaging($limit, $offset, $aCol = array(), $searchKey){
        $select = "
        a.id, a.title, a.slug, a.image, a.description, a.view_time, a.created_at,a.active_flg, a.deleted_at,
        b.name as place_name, c.image_folder,
        d.title as cate_title, d.slug as cate_slug
        ";
        if (count($aCol) > 0) {
            $select = implode(',', $aCol);
        }
        $this->db->select($select, true);
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_rao_vat_place_name . ' b', 'a.place_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'a.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->like('a.title', $searchKey);
        $this->db->limit($limit, $offset);
        return $this->db->get()->result('RaoVat_Entity');
    }

    public function getRaoVatPlace(){
        $this->load->model('entity/raovat_place_entity', 'RaoVat_Place_Entity');
        $this->db->select("a.id, a.name", true);
        $this->db->from($this->table_rao_vat_place_name . ' AS a');
        return $this->db->get()->result('RaoVat_Place_Entity');
    }

    public function insert($raovat) {
        $this->db->trans_begin();
        $this->db->insert($this->table_rao_vat_name, $raovat);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    public function update($raovat_id, $raovat) {
        $this->db->trans_begin();
        $this->db->where('id', $raovat_id);
        $this->db->update($this->table_rao_vat_name, $raovat);
        //must create new highlight record
        if(isset($raovat['highlight_flg'])){
            $update_data = array('deleted_at'=>$raovat['updated_at']);
            $this->db->where('raovat_id', $raovat_id);
            $this->db->update($this->table_rao_vat_highlight_name, $update_data);
            if($raovat['highlight_flg']==ON){
                $insert_data = array('raovat_id'=>$raovat_id, 'created_at'=>$raovat['updated_at']);
                $this->db->insert($this->table_rao_vat_highlight_name, $insert_data);
            }
        }
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    public function checkRaoVatExist($slug, $idIfUpdate=0){
        $this->db->select('a.id');
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->where('a.slug', $slug);
        if($idIfUpdate>0){
            $this->db->where('a.id <>', $idIfUpdate);
        }
        return $this->db->count_all_results() > 0 ? false:true;
    }
}
?>